function updateGridFsFiles(database) {
    db.getSiblingDB(database).fs.files.find({_id: { $type: 5}}).forEach(function(file){
        var currentId = file._id;
        var newId = ObjectId();
        db.getSiblingDB(database).fs.chunks.find({"files_id": currentId}).forEach(function(chunk){
            chunk["_id"] = chunk._id
            chunk["files_id"] = newId;
            db.getSiblingDB(database).fs.chunks.save(chunk)
        });
        
        file.metadata = { 
            "contentType": file.contentType,
            "_id": currentId
        };
        file._id = newId;
        delete file.contentType;
        db.getSiblingDB(database).fs.files.remove({_id: currentId})
        db.getSiblingDB(database).fs.files.insert(file)
    });
}
var databases = db.adminCommand({ listDatabases: 1}).databases.map(function(item){ return item.name})
var databasesWithFS = [];
databases.forEach(function(database){
    var collections = db.getSiblingDB(database).getCollectionNames().filter(function(coll) { if (coll == "fs.chunks" || coll == "fs.files") return coll })
    if (collections.length > 0)
    {
        updateGridFsFiles(database);
    }
})